class Sales::FinanceInfosController < ApplicationController
  before_action :set_sales_finance_info, only: [:show, :edit, :update, :destroy]

  # GET /sales/finance_infos
  # GET /sales/finance_infos.json
  def index
    @sales_finance_infos = Sales::FinanceInfo.all
  end

  # GET /sales/finance_infos/1
  # GET /sales/finance_infos/1.json
  def show
    @html_title = '开票信息详情'
    render layout: 'modal'
  end

  # GET /sales/finance_infos/new
  def new
    @html_title = '添加开票信息'
    @sales_finance_info = Sales::FinanceInfo.new(firm_id: params[:firm_id])
    @sales_finance_info.as_default = true if @sales_finance_info.firm.finance_infos.count==0
    render layout: 'modal'
  end

  # GET /sales/finance_infos/1/edit
  def edit
    @html_title = '开票信息编辑'
    render layout: 'modal'
  end

  # POST /sales/finance_infos
  # POST /sales/finance_infos.json
  def create
    @sales_finance_info = Sales::FinanceInfo.new(sales_finance_info_params)

    if @sales_finance_info.save
      show_js_notice '创建成功！'
      # format.html { redirect_to @sales_finance_info, notice: 'Finance info was successfully created.' }
      # format.json { render :show, status: :created, location: @sales_finance_info }
    else
      error_notice @sales_finance_info
      # format.html { render :new }
      # format.json { render json: @sales_finance_info.errors, status: :unprocessable_entity }
    end
  end

  # PATCH/PUT /sales/finance_infos/1
  # PATCH/PUT /sales/finance_infos/1.json
  def update
    if @sales_finance_info.update(sales_finance_info_params)
      show_js_notice '创建成功！'
      # format.html { redirect_to @sales_finance_info, notice: 'Finance info was successfully updated.' }
      # format.json { render :show, status: :ok, location: @sales_finance_info }
    else
      error_notice @sales_finance_info
      # format.html { render :edit }
      # format.json { render json: @sales_finance_info.errors, status: :unprocessable_entity }
    end
  end

  # DELETE /sales/finance_infos/1
  # DELETE /sales/finance_infos/1.json
  def destroy
    @sales_finance_info.destroy
    respond_to do |format|
      format.html { redirect_to sales_finance_infos_url, notice: 'Finance info was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_sales_finance_info
      @sales_finance_info = Sales::FinanceInfo.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def sales_finance_info_params
      params.require(:sales_finance_info).permit(:name, :invoice_type, :tax_num, :bank_name, :account_num, :invoice_address, :phone, :notes, :firm_id, :as_default, :active)
    end
end
